Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset uid/gid to 0 in build context to fix cache busting issues on ADD/COPY #513

Merged
merged 1 commit into from
Sep 13, 2017
Merged

Reset uid/gid to 0 in build context to fix cache busting issues on ADD/COPY #513

merged 1 commit into from
Sep 13, 2017

Conversation

shouze
Copy link
Contributor

@shouze shouze commented Sep 8, 2017

- What I did

Fix of moby/moby#32816

- How I did it

By resetting uid/gid to 0/0 in the build context. Prior to that, made possible by adding chownOpts in pkg/archive tarWithOption moby/moby#34590

- How to verify it

go test -v ./cli/command/image -run TestRunBuildResetsUidAndGidInContext

- Description for the changelog

Reset uid/gid to 0 in build context to fix cache busting issues on ADD/COPY

- A picture of a cute animal (not mandatory but encouraged)

133-insolite-25

@codecov-io
Copy link

codecov-io commented Sep 8, 2017

Codecov Report

Merging #513 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #513      +/-   ##
==========================================
+ Coverage   49.06%   49.06%   +<.01%     
==========================================
  Files         200      200              
  Lines       16407    16408       +1     
==========================================
+ Hits         8050     8051       +1     
  Misses       7938     7938              
  Partials      419      419

@shouze
Copy link
Contributor Author

shouze commented Sep 8, 2017

@tonistiigi I need to write the test but looks like not so trivial to me ATM as buildCtx is internal concept of runBuild(). Still need to read the code. I also want to address --stream, will I need to make changes inside buildkit?

@tonistiigi
Copy link
Member

@dnephin may have some tips for the test. For --stream I need to fix moby/moby#34708 first.

@dnephin
Copy link
Contributor

dnephin commented Sep 8, 2017

@tonistiigi this would be really easy to test if we merged #294 , it would be a unit test on createBuildContextFromLocalDir()

@shouze shouze changed the title Reset build ctx uid/gid to 0/0 on ADD/COPY Reset uid and gid to 0 in the build context to avoid cache busting issues on ADD/COPY Sep 9, 2017
@shouze shouze changed the title Reset uid and gid to 0 in the build context to avoid cache busting issues on ADD/COPY Reset uid/gid to 0 in build context to fix cache busting issues on ADD/COPY Sep 9, 2017
@shouze
Copy link
Contributor Author

shouze commented Sep 9, 2017

@tonistiigi @dnephin ok I've added the test, ready for a review.

@tonistiigi ok so about --stream maybe I can make a dedicated PR as it's still an experimental feature while this PR fixes a legacy issue. WDYT?

@shouze
Copy link
Contributor Author

shouze commented Sep 10, 2017

@tonistiigi I've started moby/moby#34708 in moby/moby#34797 but looks like it's probably just more than an update, I'll take a look as a fsutil upgrade seems required too.

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The test is looking good, but I have a couple suggestions for making it more readable.

The gotestyourself/fs helpers are new, so they weren't used on the existing test. I'll have to go back and update it to use the new functions.

func TestRunBuildResetsUidAndGidInContext(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Invalid test on Windows")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have some helpers for this, please use https://godoc.org/github.com/gotestyourself/gotestyourself/skip

skip.IfCondition(t, runtime.GOOS == "windows", "uid and gid not relevant on windows")

}
dest, err := ioutil.TempDir("", "test-build-context-dest")
require.NoError(t, err)
defer os.RemoveAll(dest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have helpers for this: https://godoc.org/github.com/gotestyourself/gotestyourself/fs

dest := fs.NewDir(t, "test-build-context-dest")
defer dest.Remove()

// use dest.Path() to get the path


ioutil.WriteFile(filepath.Join(dir, "foo"), []byte("some content"), 0644)
err = os.Lchown(filepath.Join(dir, "foo"), 65534, 65534)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can use a Dockerfile from the context, instead of stdin. So dockerfile can be just the string, instead of a bytes.Buffer. and you don't need to set cli.SetIn(). Below you won't need the options.dockerfileName = "-" line either.

You can replace this block (line 45-53) with:

dir := fs.NewDir(t, "test-build-context", 
    fs.WithFile("foo", "some content", AsUser(65534, 65534)),
    fs.WithFile("Dockerfile", dockerfile),
)
defer dir.Remove()

You'll have to update vendor.conf to use gotestyourself v1.2.0 as WithFile() was fixed in that version to accept PathOp

@shouze
Copy link
Contributor Author

shouze commented Sep 12, 2017

@dnephin ready for a new review, thx for gotestyourself tips, it reduces a lot the noise when we read the test 👍

I can upgrade/modernize the other tests in another PR if you want.

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test looks great, Thanks!

LGTM


fakeImageBuild := func(_ context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
buffer := new(bytes.Buffer)
tee := io.TeeReader(context, buffer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? It doesn't seem like buffer is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, removed this unused intermediate buffer var 😉


fakeImageBuild := func(_ context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
tee := io.TeeReader(context, new(bytes.Buffer))
assert.NoError(t, archive.Untar(tee, dest.Path(), nil))
Copy link
Contributor

@dnephin dnephin Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can pass context here as the first argument to Untar() and remove tee altogether, no?

The reason I'm using io.TeeReader() in the other test is because I want to assert the header is as expected, but in this case the test isn't concerned with the compression, it just wants the files, so I don't think you need the extra buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, done!

@tonistiigi
Copy link
Member

LGTM

@shouze Needs rebase

@shouze
Copy link
Contributor Author

shouze commented Sep 13, 2017

@tonistiigi thx, rebased!

@shouze
Copy link
Contributor Author

shouze commented Sep 13, 2017

@dnephin @tonistiigi I guess that after the merge a documentation upgrade would be required?

@dnephin dnephin merged commit 7b77ab5 into docker:master Sep 13, 2017
@GordonTheTurtle GordonTheTurtle added this to the 17.10.0 milestone Sep 13, 2017
@shouze shouze deleted the reset-id-pair-during-build-to-avoid-cache-busting branch September 13, 2017 19:21
@thaJeztah
Copy link
Member

/cc @mstanleyjones for documentation ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants